<?php
namespace Services;

use Helpers\Tool;
use Helpers\Main;
use Helpers\HttpClient;
use Helpers\XhException;

class App
{

    /**
     * @var \Helpers\HttpClient
     */
    public static $httpClient;
    public static $url;
    public static $versionNum;

    const KEYWORD = 'app.yunzuoye.net&^%$';

    public static $enum = [
        'userType'          => [1 => '学生', 2 => '教师', 3 => '家长', 4 => '通用'],
        'installPosition'   => [1 => 'SD卡', 2 => '系统目录'],
        'network'           => [1 => '3GWIFI', 2 => 'WIFI', 3 => '3G', 4 => '禁用'],
        'updateLevel'       => [1 => '无需更新', 2 => '推荐更新', 3 => '强制更新'],
        'programType'       => [0 => '应用', 1 => '插件', 2 => '外部应用'],
        'permission'        => [0 => '全部禁止', 1 => '允许清数据', 2 => '允许卸载', 3 => '全部允许'],
        'status'            => [0 => '删除', 1 => '上架', 2 => '禁用', 3 => '下架', 4 => '待审核', 5 => '已拒绝'],
    ];
    public static $boolFields = ['independent', 'outside', 'show'];
    public static $timeFields = ['createTime', 'updateTime', /*'verCreateTime'*/ 'updatedAt'];
    public static $intFields  = [
        'userType',
        'installPosition',
        'network',
        'updateLevel',
        'programType',
        'permission',
        'status',
        'order',
        'packageSize'
    ];


    /**
     * 初始化配置
     *
     * @throws XhException
     *
     * create by ck 20180507
     */
    private static function init()
    {
        if (!self::$httpClient) {
            $config     = Main::getConfigByName('jAppServer');
            self::$url  = Tool::getValue($config, 'url');
            if (!self::$url) {
                throw new XhException('应用服务url获取失败');
            }
            self::$versionNum = Tool::getValue($config, 'number', 1);
            self::$httpClient = new HttpClient(self::$url);
        }
    }

    /**
     * 获取应用列表
     *
     * @param int $page
     * @param int $size
     * @param array $extraParams
     * @return array
     * @throws XhException
     *
     * create by ck 20180507
     */
    public static function getApplications($page = 1, $size = 50, $extraParams = [])
    {
        self::init();
        $response = self::$httpClient->get(
            '/api/v1/applications/',
            array_merge(['page' => $page, 'size' => $size], $extraParams)
        );
        $body = $response->toArray();
        if (!$body || $body['totalElements'] === null || $body['content'] === null) {
            throw new XhException('无返回数据', $response);
        }
        $total = $body['totalElements'];
        $data  = $body['content'];
        return ['total' => $total, 'rows' => $data];
    }

    /**
     * 上传应用
     * @param $appInfo
     * @return mixed
     *
     * create by fyh 20181213
     */
    public static function getUploadApplication($appInfo)
    {
        self::init();
        $response = self::$httpClient->get(sprintf('/api/v1/applications/%s/info', $appInfo['packageName']));
        if ($response->isSuccess() && $response->body) {
            $body = $response->toArray();
            $appInfo['appCode'] = $body['appCode'];
            $appInfo['userType'] = $body['userType'];
            $appInfo['installPosition'] = $body['installPosition'];
            $appInfo['description'] = $body['description'];
            $appInfo['show'] = $body['show'] ? 'true' : 'false';
            $appInfo['outside'] = $body['outside'] ? 'true' : 'false';
            $appInfo['independent'] = $body['independent'] ? 'true' : 'false';
            $appInfo['order'] = $body['order'];
            $appInfo['network'] = $body['network'];
        }
        return $appInfo;
    }

    /**
     * 获取应用枚举列表 包名=>名称
     *
     * @param bool $withType
     * @return array
     *
     * create by ck 20180507
     */
    public static function getAppNames($withType = true)
    {
        $apps = self::getApplications(1, 10000)['rows'];
        if ($withType) {
            $apps = array_map(function ($app) {
                $app['name'] .= '(' . self::$enum['userType'][$app['userType']] . ')';
                if ($app['platform'] != 'android') {
                    $app['name'] .= '-' . $app['platform'];
                }
                return $app;
            }, $apps);
        }
        return array_column($apps, 'name', 'packageName');
    }

    /**
     * 获取应用Appcode=>name 枚举列表
     *
     * @return array
     * @throws XhException or array
     *
     * create by qgz 20180613
     */
    public static function getAppCodes()
    {
        $apps = self::getApplications(1, 10000)['rows'];
        $appCodes = [];
        foreach ($apps as $app) {
            if (!$app['appCode'] || isset($appCodes[$app['appCode']])) {
                continue;
            }
            $appCodes[$app['appCode']] = $app['name'];
        }
        return $appCodes;
    }

    /**
     * 处理应用版本，改为多条记录
     *
     * @param $applications
     * @param bool $all
     * @return array
     *
     * create by ck 20180625
     */
    public static function runVersion(&$applications, $all = false)
    {
        if (!$all) {
            foreach ($applications as $index => &$row) {
                $verInfo              = $row['appVersions'][0];
                $verInfo['statement'] = $row['description'];
                unset($verInfo['description'], $verInfo['createTime'], $row['appVersions']);
                $row = array_merge($row, $verInfo);
            }
            unset($row);
        } else {
            $data = [];
            foreach ($applications as $index => $row) {
                $versions = $row['appVersions'];
                unset($row['appVersions']);
                foreach ($versions as $verIndex => $ver) {
                    $data[] = array_merge($row, $ver);
                }
            }
            $applications = $data;
        }

        return $applications;
    }

    /**
     * 处理枚举
     *
     * @param $applications
     * @return array
     *
     * create by ck 20180625
     */
    public static function runEnum(&$applications)
    {
        foreach ($applications as &$row) {
            foreach (self::$boolFields as $field) {
                $row[$field] = Tool::getValue($row, $field, false) ? '是' : '否';
            }
            isset($row['url']) and $row['url'] = "<a target='_blank' href='{$row['url']}'>{$row['url']}</a>";
        }
        unset($row);
        Tool::runEnum($applications, self::$enum);
        Tool::runTimestamp($applications, self::$timeFields);
        Tool::runByteToMByte($applications, 'packageSize');
        return $applications;
    }

    /**
     * 根据包名获取应用数据
     *
     * @param $packageName
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by ck 20180528
     */
    public static function getAppInfoByPackage($packageName)
    {
        self::init();
        $response = self::$httpClient->get(sprintf('/api/v1/applications/%s/info', $packageName));
        if ($response->header->statusCode != 200) {
            throw new XhException('获取应用详情失败', $response);
        }
        return $response->toArray();
    }

    /**
     * 根据appId获取应用数据
     *
     * @param $appId
     * @param $number
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by ck 20180528
     * modify by dale 20180829
     */
    public static function getAppInfoById($appId, $number = null)
    {
        self::init();
        $param = [];
        if ($number) {
            $param['number'] = $number;
        }
        $response = self::$httpClient->get(sprintf('/api/v1/applications/%s', $appId), $param);
        if (!$response->isSuccess()) {
            throw new XhException('获取应用详情失败', $response);
        }
        return $response->toArray();
    }

    /**
     * 发布应用
     * @param $appInfo
     * @throws XhException
     *
     * create by fyh 20181213
     */
    public static function releaseApplication($appInfo)
    {
        self::init();
        self::$httpClient->setHeader('Content-Type', 'application/json');
        $response = self::$httpClient->postJson(
            sprintf('/api/v1/applications/%s/version', $appInfo['packageName']),
            $appInfo
        );
        if (!$response->isSuccess()) {
            throw new XhException('发布失败', $response);
        }
    }

    /**
     * 删除应用
     * @param $ids
     * @return mixed
     * @throws XhException
     *
     * create by fyh 20181213
     */
    public static function delApplication($ids)
    {
        self::init();
        $response = self::$httpClient->delete(sprintf('/api/v1/applications/%s', $ids[0]));
        $result['code'] = 0;
        if (!$response->isSuccess()) {
            throw new XhException('删除失败', $response);
        }
        return $result;
    }

    /**
     * 同步应用到其他平台
     * @param $ids
     * @param $synUrl
     * @param $key
     * @return mixed
     * @throws XhException
     *
     * reate by fyh 20181213
     */
    public static function synApplication($ids, $synUrl, $key)
    {
        self::init();
        $response = self::$httpClient->get(sprintf('/api/v1/applications/%s', $ids[0]));
        if (!$response->isSuccess()) {
            $body = $response->toObject();
            throw new XhException('数据获取失败', $body->error ?: $response);
        }
        $body    = $response->toArray();
        $appInfo = [
            'appCode'           => $body['appCode'],
            'name'              => $body['name'],
            'platform'          => $body['platform'],
            'packageName'       => $body['packageName'],
            'userType'          => $body['userType'],
            'icon'              => $body['icon'],
            'description'       => $body['description'],
            'installPosition'   => $body['installPosition'],
            'network'           => $body['network'],
            'independent'       => $body['independent'] ? 'true' : 'false',
            'show'              => $body['show'] ? 'true' : 'false',
            'outside'           => $body['outside'] ? 'true' : 'false',
            'order'             => $body['order'],
            'updateLevel'       => $body['updateLevel'],
            'url'               => $body['appVersions'][0]['url'],
            'md5'               => $body['appVersions'][0]['md5'],
            'packageSize'       => $body['appVersions'][0]['packageSize'],
            'plistUrl'          => $body['appVersions'][0]['plistUrl'],
            'versionCode'       => $body['appVersions'][0]['versionCode'],
            'versionName'       => $body['appVersions'][0]['versionName'],
            'statement'         => $body['appVersions'][0]['description'],
        ];
        $key = strtoupper(md5(date('Ymd') . '+' . $key));
        $appInfo['key'] = $key;
        self::$httpClient->setBaseUrl($synUrl);
        $response = self::$httpClient->post('/Appserver/releaseApplication', $appInfo);
        if (!$response->isSuccess() || !$body->msg) {
            $body = $response->toObject();
            throw new XhException('同步失败', $body->error ?: $response);
        }
    }

    /**
     * 下架应用
     * @param $packageName
     * @throws XhException
     *
     * create by fyh 20181213
     */
    public static function setApplicationOffShelf($packageName)
    {
        self::init();
        $response = self::$httpClient->put(sprintf('/api/v1/applications/%s/offshelf', $packageName), [
            'keyword' => 'app.yunzuoye.net&^%$']);
        if (!$response->isSuccess()) {
            $body = $response->toObject();
            throw new XhException('下架失败', $body->error ?: $response);
        }
    }

    /**
     * 更新应用到学校
     * @param $packageName
     * @param $versionCode
     * @param $schoolIds
     * @param $operate
     * @throws XhException
     *
     * create by fyh 20181213
     */
    public static function addAppToSchool($packageName, $versionCode, $schoolIds, $operate)
    {
        self::init();
        $response = self::$httpClient->postJson(
            sprintf('/api/v1/apps/%s/versions/%s/schools?operate=%s', $packageName, $versionCode, $operate),
            ['schools' => implode(',', $schoolIds)]
        );
        if (!$response->isSuccess()) {
            $body = $response->toObject();
            throw new XhException('更新应用到学校失败', $body->error ?: $response);
        }
    }

    /**
     * 修改应用基本信息
     *
     * @param $appId
     * @param $appInfo
     * @throws XhException
     *
     * create by ck 20181016
     */
    public static function modifyAppBase($appId, $appInfo)
    {
        self::init();
        Tool::runInteger($appInfo, self::$intFields);
        Tool::runBoolean($appInfo, self::$boolFields);
        $response = self::$httpClient->putJson(
            sprintf('/api/v1/applications/%s', $appId),
            $appInfo
        );
        if (!$response->isSuccess()) {
            throw new XhException('修改应用信息失败', $response);
        }
    }

    /**
     * 获取应用指定版本信息
     *
     * @param $appId
     * @param $versionCode
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by ck 20181018
     */
    public static function getAppVersionInfo($appId, $versionCode)
    {
        self::init();
        $response = self::$httpClient->get(
            sprintf('/api/v1/applications/%s/versions/%d/info', $appId, $versionCode)
        );
        if (!$response->isSuccess()) {
            throw new XhException('获取版本信息失败', $response);
        }
        return $response->toArray();
    }

    /**
     * 修改应用版本信息
     *
     * @param $appId
     * @param $versionCode
     * @param $versionInfo
     * @throws XhException
     *
     * create by ck 20181018
     */
    public static function modifyAppVersion($appId, $versionCode, $versionInfo)
    {
        self::init();
        Tool::runInteger($versionInfo, self::$intFields);
        $response = self::$httpClient->putJson(
            sprintf('/api/v1/applications/%s/versions/%d', $appId, $versionCode),
            $versionInfo
        );
        if (!$response->isSuccess()) {
            throw new XhException('获取版本信息失败', $response);
        }
    }

    /**
     * 获取应用组
     *
     * @param int $page
     * @param int $size
     * @param array $params
     * @return array
     * @throws XhException
     *
     * create by ck 20180528
     */
    public static function getAppGroups($page = 1, $size = 50, $params = [])
    {
        self::init();
        $response = self::$httpClient->get(
            '/api/v1/groups/search',
            array_merge(['page' => $page, 'size' => $size], $params)
        );
        if (!$response->isSuccess()) {
            throw new XhException('获取应用组失败', $response);
        }
        $body     = $response->toArray();
        return ['total' => $body['totalElements'], 'rows' => $body['content']];
    }

    /**
     * 创建应用组
     *
     * @param $groupName
     * @param $schoolId
     * @param int $userType
     * @return string
     * @throws XhException
     *
     * create by ck 20180528
     */
    public static function createAppGroup($groupName, $schoolId, $userType = 4)
    {
        self::init();
        $response = self::$httpClient->fullResponse()->postJson(
            '/api/v1/groups/',
            ['groupName' => $groupName, 'schoolId' => $schoolId, 'userType' => $userType]
        );
        if (!$response->isSuccess()) {
            throw new XhException('创建应用组失败', $response);
        }
        return basename($response->responseHeader['location']);
    }

    /**
     * 获取组内用户
     *
     * @param $groupId
     * @return array
     * @throws XhException
     *
     * create by ck 20180730
     */
    public static function getAppGroupUser($groupId)
    {
        self::init();
        $response = self::$httpClient->get(sprintf('/api/v1/groups/%s', $groupId));
        if (!$response->isSuccess()) {
            throw new XhException('获取应用组用户失败', $response);
        }
        $userIds = array_column($response->toArray()['userIdList'], 'id') ?: [];
        return $userIds;
    }

    /**
     * 修改应用组用户
     *
     * @param $groupId
     * @param array $userIds
     * @throws XhException
     *
     * create by ck 20180528
     */
    public static function modifyAppGroupUser($groupId, $userIds = [])
    {
        self::init();
        //用户格式处理
        if (!is_array($userIds[0])) {
            $userIds = array_map(function ($userId) {
                return ['id' => $userId];
            }, $userIds);
        }
        $response = self::$httpClient->putJson(sprintf('/api/v1/groups/%s', $groupId), ['userIdList' => $userIds]);
        if (!$response->isSuccess()) {
            throw new XhException('应用组用户修改失败', $response);
        }
    }

    /**
     * 应用组加应用
     *
     * @param $groupId
     * @param array $apps
     * @throws XhException
     *
     * create by ck 20180528
     */
    public static function addAppGroupApp($groupId, $apps = [])
    {
        if (!$apps) {
            throw new XhException('缺少应用数据');
        }
        self::init();
        $response = self::$httpClient->postJson(sprintf('/api/v1/initiate/groups/%s/apps', $groupId), $apps);
        if (!$response->isSuccess()) {
            throw new XhException('应用组应用增加失败', $response);
        }
    }

//    public static function getApplications($page = 1, $size = 50, $params = [])
//    {
//        $apps = self::getAppSource($page, $size, $params);
//
//        //处理版本信息
//        foreach($apps['rows'] as $index => &$row){
//            $verInfo                  = $row['appVersions'][0];
//            $verInfo['statement']     = $verInfo['description'];
//            unset($verInfo['description'], $verInfo['createTime'], $row['appVersions']);
//            $row = array_merge($row, $verInfo);
//        }
//        unset($row);
//
//        $this->runApplicationEnum($data);
//        return $this->sendJsonResponse(['total' => $total, 'rows' => $data]);
//    }

    /**
     * 获取黑名单
     *
     * @param int $page
     * @param int $size
     * @param array $params
     * @return array
     * @throws XhException
     *
     * create by xjq 20180614
     */
    public static function getAppBlacklists($page = 1, $size = 50, $params = [])
    {
        self::init();
        $response = self::$httpClient->get(
            '/api/v1/blacklist/',
            array_merge(['page' => $page, 'size' => $size], $params)
        );
        $body     = $response->toArray();
        if (!$response->isSuccess()) {
            throw new XhException('获取黑名单失败', $response);
        }
        return ['total' => $body['totalElements'], 'rows' => $body['content']];
    }

    /**
     * 添加应用黑名单
     *
     * @param array $data
     * @throws XhException
     *
     * create by xjq 20180614
     */
    public static function addAppBlacklist($data = [])
    {
        if (!$data) {
            throw new XhException('没有数据传入');
        }
        self::init();
        $response = self::$httpClient->postJson('/api/v1/blacklist/', $data);
        if (!$response->isSuccess()) {
            throw new XhException('应用黑名单增加失败', $response);
        }
    }

    /**
     * 删除黑名单
     *
     * @param $id
     * @throws XhException
     *
     * create by xjq 20180614
     */
    public static function delAppBlacklist($id)
    {
        if (!$id) {
            throw new XhException('缺少id');
        }
        self::init();
        $response = self::$httpClient->delete(sprintf('/api/v1/blacklist/%s', $id));
        if (!$response->isSuccess()) {
            throw new XhException('应用黑名单删除失败', $response);
        }
    }

    /**
     * 改变黑名单状态
     *
     * @param $id
     * @param $params
     * @throws XhException
     *
     * create by xjq 20180614
     */
    public static function changeAppBlacklistStatus($id, $params = [])
    {
        if (!$id) {
            throw new XhException('缺少id');
        }
        self::init();
        $response = self::$httpClient->put(sprintf('/api/v1/blacklist/%s', $id), $params);
        if (!$response->isSuccess()) {
            throw new XhException('应用黑名单删除失败', $response);
        }
    }

    /**
     * 获取学校防火墙
     *
     * @param $page
     * @param $size
     * @param array $params
     * @return array
     * @throws XhException
     *
     * create by xjq 20180614
     */
    public static function getSchoolFirewall($page, $size, $params = [])
    {
        self::init();
        $response = self::$httpClient->get(
            '/api/v1/schools/firewall/blacklist/apps',
            array_merge(['page' => $page, 'size' => $size], $params)
        );
        if (!$response->isSuccess()) {
            throw new XhException('获取学校防火墙失败', $response);
        }
        $body = $response->toArray();
        return ['total' => $body['totalElements'], 'rows' => $body['content']];
    }

    /**
     * 新增学校防火墙
     *
     * @param array $params
     * @throws XhException
     *
     * create by xjq 20180614
     */
    public static function addSchoolFirewall($params = [])
    {
        self::init();
        $response = self::$httpClient->postJson('/api/v1/schools/firewall/blacklist/apps', $params);
        if (!$response->isSuccess()) {
            throw new XhException('学校防火墙新增失败', $response);
        }
    }

    /**
     * 添加全部学校防火墙
     *
     * @param array $params
     * @throws XhException
     * create by xjq 20180706
     */
    public static function addAllSchoolFirewall($params = [])
    {
        self::init();
        $response = self::$httpClient->postJson('/api/v1/schools/ALL/firewall/blacklist/apps', $params);
        if (!$response->isSuccess()) {
            throw new XhException('学校防火墙新增失败', $response);
        }
    }

    /**
     * 更新学校防火墙
     *
     * @param $schoolId
     * @param $packageName
     * @param array $params
     * @throws XhException
     *
     * create by xjq 20180614
     */
    public static function modifySchoolFirewall($schoolId, $packageName, $params = [])
    {
        if (!$schoolId || !$packageName) {
            throw new XhException('缺少参数');
        }
        self::init();
        $response = self::$httpClient->putJson(
            sprintf('/api/v1/schools/%s/firewall/blacklist/apps/%s', $schoolId, $packageName),
            $params
        );
        if (!$response->isSuccess()) {
            throw new XhException('学校防火墙修改失败', $response);
        }
    }

    /**
     * 删除学校防火墙
     *
     * @param array $ids
     * @throws XhException
     *
     * create by xjq 20180614
     */
    public static function delSchoolFirewall($ids = [])
    {

        self::init();
        $response = self::$httpClient->setBodyParams($ids, true)
            ->delete('/api/v1/schools/firewall/blacklist/apps');
        if (!$response->isSuccess()) {
            throw new XhException('学校防火墙删除失败', $response);
        }
    }

    /**
     * 获取待上架的应用
     *
     * @param int $page
     * @param int $size
     * @param array $params
     * @return array
     * @throws XhException
     *
     * create by xjq 20180619
     */
    public static function getAppStayShelfLists($page = 1, $size = 50, $params = [])
    {
        self::init();
        $response = self::$httpClient->get(
            '/api/v1/applications/versions/stayShelf',
            array_merge(['page' => $page, 'size' => $size], $params)
        );
        if (!$response->isSuccess()) {
            throw new XhException('获取待上架的应用失败', $response);
        }
        $body = $response->toArray();
        return ['total' => $body['totalElements'], 'rows' => $body['content']];
    }

    /**
     * 同意上架
     *
     * @param $appId
     * @param $versionCode
     * @param array $params
     * @throws XhException
     *
     * create by xjq 20180619
     */
    public static function agreeAppStayShelf($appId, $versionCode, $params = [])
    {
        if (!$appId || !$versionCode) {
            throw new XhException('缺少参数');
        }
        self::init();
        $response = self::$httpClient->patch(
            sprintf('/api/v1/applications/%s/versions/%s/agree', $appId, $versionCode),
            $params
        );
        if (!$response->isSuccess()) {
            throw new XhException('同意应用上架失败', $response);
        }
    }

    /**
     * 拒绝上架
     *
     * @param $appId
     * @param $versionCode
     * @param array $params
     * @throws XhException
     *
     * create by xjq 20180619
     */
    public static function refuseAppStayShelf($appId, $versionCode, $params = [])
    {
        if (!$appId || !$versionCode) {
            throw new XhException('缺少参数');
        }
        self::init();
        $response = self::$httpClient->patch(
            sprintf('/api/v1/applications/%s/versions/%s/refuse', $appId, $versionCode),
            $params
        );
        if (!$response->isSuccess()) {
            throw new XhException('拒绝应用上架失败', $response);
        }
    }

    /**
     * 获取下架的应用
     *
     * @param int $page
     * @param int $size
     * @param array $params
     * @return array
     * @throws XhException
     *
     * create by xjq 20180619
     */
    public static function getAppOffShelfLists($page = 1, $size = 50, $params = [])
    {
        self::init();
        $response = self::$httpClient->get(
            '/api/v1/applications/offshelf',
            array_merge(['page' => $page, 'size' => $size], $params)
        );
        $body = $response->toArray();
        if (!$response->isSuccess()) {
            throw new XhException('获取下架的应用失败', $response);
        }
        return ['total' => $body['totalElements'], 'rows' => $body['content']];
    }


    /**
     * 下架应用恢复
     *
     * @param $packageName
     * @param $version
     * @throws XhException
     *
     * create by xjq 20180619
     */
    public static function recoverOffShelf($packageName, $version)
    {
        if (!$packageName || !$version) {
            throw new XhException('缺少参数');
        }
        self::init();
        $response = self::$httpClient->put(
            sprintf('/api/v1/applications/%s/offshelf/recover', $packageName),
            ['keyword' => self::KEYWORD, 'version' => $version]
        );
        if (!$response->isSuccess()) {
            throw new XhException('下架应用恢复失败', $response);
        }
    }

    /**
     * 获取学校应用学生安装详情
     *
     * @param $schoolId
     * @param $packageName
     * @return array
     * @throws XhException
     *
     * create by ck 20180626
     */
    public static function getSchoolAppInstallDetail($schoolId, $packageName)
    {
        if (!$packageName || !$schoolId) {
            throw new XhException('缺少参数');
        }
        self::init();
        $response = self::$httpClient->get(
            sprintf('/api/v1/schools/%d/packageName/%s/install/details', $schoolId, $packageName)
        );
        if (!$response->isSuccess()) {
            throw new XhException('获取学校指定应用学生安装详情失败', $response);
        }
        return $response->toArray();
    }

    /**
     * 获取匹配应用->反馈
     *
     * @param $userType
     * @param $platform
     * @param $programType
     *
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by dale  20180823
     */
    public static function getAppMatch($userType = null, $platform = null, $programType = null)
    {
        self::init();
        $param = [];
        if ($userType) {
            $param['userType'] = $userType;
        }
        if ($platform) {
            $param['platform'] = $platform;
        }
        if ($programType) {
            $param['programType'] = $programType;
        }
        $response = self::$httpClient->get('/api/v1/applications/match', $param);
        if (!$response->isSuccess()) {
            throw new XhException('获取匹配应用失败', $response);
        }
        return $response->toArray();
    }
}